package com.centaline.androidsalesblog.widgets;

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.constants.DefaultHint;

/**
 * 列表搜索控件
 * Created by guilin on 16/5/4.
 */
public class ListSearchView extends FrameLayout implements View.OnClickListener {

    private AppCompatTextView atv_default_title;
    private ImageView img_list_mode;
    private RelativeLayout rl_list_search;
    private AppCompatTextView atv_list_content;
    private ImageView img_search_clear;
    private ImageView img_search_voice;

    private int listMapMode = 0;//0:列表模式;1:地图模式
    private DefaultHint defaultHint;
    private SearchCallback searchCallback;
    private ModeCallback modeCallback;

    public ListSearchView(Context context) {
        this(context, null);
    }

    public ListSearchView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ListSearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.layout_list_search, this);
        atv_default_title = (AppCompatTextView) findViewById(R.id.atv_default_title);
        img_list_mode = (ImageView) findViewById(R.id.img_list_mode);
        img_list_mode.setOnClickListener(this);
        rl_list_search = (RelativeLayout) findViewById(R.id.rl_list_search);
        rl_list_search.setOnClickListener(this);
        atv_list_content = (AppCompatTextView) findViewById(R.id.atv_list_content);
        img_search_clear = (ImageView) findViewById(R.id.img_search_clear);
        img_search_clear.setOnClickListener(this);
        img_search_voice = (ImageView) findViewById(R.id.img_search_voice);
        img_search_voice.setOnClickListener(this);
    }

    /**
     * 设置回调
     */
    public void setSearchCallback(SearchCallback searchCallback) {
        this.searchCallback = searchCallback;
    }

    /**
     * 模式回调
     */
    public void setModeCallback(ModeCallback modeCallback) {
        this.modeCallback = modeCallback;
    }

    /**
     * 设置搜索默认标题
     */
    public void setSearchDefaultTitle(CharSequence title) {
        if (TextUtils.isEmpty(title))
            return;
        atv_default_title.setText(title);
        atv_default_title.setVisibility(VISIBLE);
        rl_list_search.setVisibility(GONE);
    }

    /**
     * 设置默认显示文本
     */
    public void setDefaultHint(DefaultHint defaultHint) {
        this.defaultHint = defaultHint;
        setContentText(defaultHint.getHintText());
    }

    /**
     * 设置搜索内容
     */
    public void setSearchContent(CharSequence text) {
        img_search_clear.setVisibility(VISIBLE);
        img_search_voice.setVisibility(GONE);
        setContentText(text);

    }

    private void setContentText(CharSequence text) {
        atv_list_content.setText(text);
    }

    /**
     * 还原默认显示文本
     */
    private void resetDefaultHint() {
        setContentText(defaultHint.getHintText());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_list_mode:
                listMapMode = listMapMode == 0 ? 1 : 0;
                img_list_mode.setImageResource(listMapMode == 0 ?
                        R.drawable.ic_menu_map : R.drawable.ic_menu_list);
                modeCallback.mode(listMapMode);
                break;
            case R.id.rl_list_search:
                searchCallback.gotoSearch();
                break;
            case R.id.img_search_clear:
                resetDefaultHint();
                img_search_clear.setVisibility(GONE);
                img_search_voice.setVisibility(VISIBLE);
                searchCallback.clear();
                break;
            case R.id.img_search_voice:
                searchCallback.gotoSearchVoice();
                break;
            default:
                break;
        }
    }

    public interface SearchCallback {

        /**
         * 清除操作
         */
        void clear();

        /**
         * 去搜索
         */
        void gotoSearch();

        /**
         * 语音搜索
         */
        void gotoSearchVoice();
    }

    public interface ModeCallback {
        /**
         * 模式切换
         */
        void mode(int mode);
    }
}
